if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.envDef.envDtls.shdResDefs.ShdResDefs")) {

    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.envDef.envDtls.shdResDefs", function (pkgshd) {
    });

    /**
     * Shared Resource Definitions
     * Felix 6/25/2007
     */

    /**
     * EnterpriseAssets
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.envDef.envDtls.shdResDefs.ShdResDefs",
            jsx3.lang.Object,
            [tibco.admin.enterpriseAssets.envDef.envDtls.EnvDtlsNavInterface, tibco.admin.plugin.PluginContainerInterface],
            function (plugin) {

                plugin.imagesForType = new Object();
                plugin.stringsForType = new Object();

                plugin.prototype.subscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, me, me.onEnvironmentSelect);
                }

                plugin.prototype.unsubscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, me);
                }

                plugin.prototype.mode = "VIEW";

                plugin.instance = null;
                plugin.getInstance = function(args) {
                    if(!plugin.instance) {
                        plugin.instance = new tibco.admin.enterpriseAssets.envDef.envDtls.shdResDefs.ShdResDefs();
                    }
                    return plugin.instance;
                };

                plugin.prototype.setupPlugin = function(obj) {

                }

                plugin.prototype.getImageForType = function(type) {
                    var ret = plugin.imagesForType[type];
                    if(!ret) {
                        var iter = this.getPluginList().iterator();
                        while(iter.hasNext()) {
                            var item = iter.next();
                            if(item.getType() == type) {
                                ret = "" + item.getImagePath();
                                if(!ret) {
                                    ret = tibco.admin.Main.getServer().getDynamicProperty("@addin@Blank Icon Path");
                                }
                                else {
                                    plugin.imagesForType[type] = ret;
                                }
                            }
                        }
                    }
                    if(!ret) {
                        ret = tibco.admin.Main.getServer().getDynamicProperty("@addin@Blank Icon Path");
                    }
                    return ret;
                }

                plugin.prototype.getDisplayStringForType = function(type) {
                    var ret = plugin.stringsForType[type];
                    if(!ret) {
                        var iter = this.getPluginList().iterator();
                        while(iter.hasNext()) {
                            var item = iter.next();
                            if(item.getType() == type) {
                                ret = "" + item.getAddText();
                                if(!ret) {
                                    ret = type
                                }
                                else {
                                    plugin.stringsForType[type] = ret;
                                }
                            }
                        }
                    }
                    if(!ret) {
                        ret = type;
                    }
                    return ret;
                }

                plugin.prototype.getNamespace = function() {
                    return "tibco.admin";
                };
                plugin.prototype.loadComponent = function(parent) {
                    // AK: this code moved to onBeforeSerailize of genInfo.xml UI resource
                    var me = this;
                    tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/ui/shdResDefs.xml", me, me.componentLoaded);
                };

                plugin.prototype.componentLoaded = function(objEvent) {
                    if(objEvent.status == "SUCCESS") {
                        this.block = objEvent.obj;
                        this.block.getDescendantOfName("mcUse_27I").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@ShdResDefs Enabled View Mode Value Template"));
                        this.block.handler = this;
                        this.mtx = this.block.getDescendantOfName("mtxShdRedDefsList_27I");
                        this.selectEnable = this.block.getDescendantOfName("selFilterEnable27I");
                        this.selectEnable.setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                        //subscribe to relevant events
                        this.subscribeAll();

                    }
                    else {
                        if(objEvent.exception)
                        {
                            tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                        }
                        else {
                            jsx3.log(objEvent.message);
                        }
                    }
                }

                //interface method
                plugin.prototype.getNavLinkText = function() {
                    return tibco.admin.Main.getServer().getDynamicProperty("@@NavLinkTextShdResDefs");
                }
                // interface method
                plugin.prototype.getPathToDetailsBlock = function() {
                    return tibco.admin.Main.resolveURI("tibco.admin", "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/ui/shdResDefs.xml");
                }

                //called whenever one of the environments in the evironments list is selected
                plugin.prototype.onEnvironmentSelect = function(objEvent) {
                    if(this.isInEditMode()) {
                        return;
                    }
                    if(objEvent.environmentName) {
                        this.entityId = objEvent.environmentID;
                        if(!tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails) {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ShdResDefDetails.js",
                                    "tibco.admin");
                        }
                        if(this.getPluginList().size() == 0) {
                            var preLoad = new jsx3.util.List(0);

                            // load bundled shared resource definitions
                            // TODO: add RV, SSL
                            if(!tibco.admin.enterpriseAssets.shdResDefs.HTTP || !tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/HTTP/ShdHttpResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefs, preLoad.size());
                            }
                            if(!tibco.admin.enterpriseAssets.shdResDefs.JDBC || !tibco.admin.enterpriseAssets.shdResDefs.JDBC.ShdJdbcResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/JDBC/ShdJdbcResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.JDBC.ShdJdbcResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.JDBC.ShdJdbcResDefs, preLoad.size());
                            }
                            if(!tibco.admin.enterpriseAssets.shdResDefs.EMS || !tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/EMS/ShdEmsResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefs, preLoad.size());
                            }
                            if(!tibco.admin.enterpriseAssets.shdResDefs.JNDI || !tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/JNDI/ShdJndiResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.JNDI.ShdJndiResDefs, preLoad.size());
                            }
                            if(!tibco.admin.enterpriseAssets.shdResDefs.RV || !tibco.admin.enterpriseAssets.shdResDefs.RV.ShdRVResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/RV/ShdRVResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.RV.ShdRVResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.RV.ShdRVResDefs, preLoad.size());
                            }
                            if(!tibco.admin.enterpriseAssets.shdResDefs.ServerSSL || !tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ServerSSL/ShdSSLServerResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ServerSSL.ShdSSLServerResDefs, preLoad.size());
                            }
                            /* remove SSL client
                            if (!tibco.admin.enterpriseAssets.shdResDefs.ClientSSL || !tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/ShdSSLClientResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefs, preLoad.size());
                            }*/
                            if(!tibco.admin.enterpriseAssets.shdResDefs.Identity || !tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefs) {
                                try {
                                    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/Identity/ShdIdentityResDefs.js", this.getNamespace());
                                    preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefs, preLoad.size());
                                }
                                catch(ex) {
                                    tibco.admin.Main.logException(ex);
                                }
                            }
                            else {
                                preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefs, preLoad.size());
                            }
                            this.loadPlugins("tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface", preLoad);
                        }
                        else {
                            this.onAfterGetPlugins();
                        }
                    }
                    else {
                        this.clearAll();
                    }
                };

                plugin.prototype.onAfterGetPlugins = function() {
                    this.initTypeFilter();
                    this.callgetResourceConfigsInEnvironment();
                }

                plugin.prototype.initTypeFilter = function() {
                    var strAllTypes = tibco.admin.Main.getServer().getDynamicProperty("@AllTypes");
                    this.selectFilter = this.block.getDescendantOfName("selFilterType27I");
                    this.selectFilter.setValue(strAllTypes);
                    this.selectFilter.insertRecord({jsxid:strAllTypes, jsxtext:strAllTypes}, null, true);
                    var iter = this.getPluginList().iterator();
                    while(iter.hasNext()) {
                        var obj = iter.next();
                        this.selectFilter.insertRecord({jsxid:obj.getType(), jsxtext:obj.getAddText()}, null, true);
                    }
                }

                plugin.prototype.clearAll = function() {
                    this.toggleMode(false);
                    tibco.admin.Main.getServer().getCache().clearById(this.mtx.getXMLId());
                    this.mtx.repaint();
                    this.block.getDescendantOfName("btnEdit_27I").setEnabled(jsx3.gui.Form.STATEDISABLED, true)
                    this.selectFilter.setEnabled(jsx3.gui.Form.STATEDISABLED, true).setValue(tibco.admin.Main.getServer().getDynamicProperty("@AllTypes"));
                    this.selectEnable.setEnabled(jsx3.gui.Form.STATEDISABLED, true).setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                }

                // called when require create new environment event happened
                plugin.prototype.onNewEnvironment = function(objEvent) {

                };

                // called when the edit button clicked
                plugin.prototype.doEdit = function() {
                    this.toggleMode(true);
                    this.mode = "EDIT";
                };

                // called when the cancel button clicked
                plugin.prototype.doCancel = function() {
                    this.toggleMode(false);
                    this.mode = "VIEW";
                    this.callgetResourceConfigsInEnvironment();
                };

                // called when the save button clicked
                plugin.prototype.doSave = function() {
                    var server = tibco.admin.Main.getServer();
                    // Clone a new document to populate the records which are associated with the Env record.
                    var cdfMtxNew = this.mtx.getXML().cloneDocument();
                    var cacheMtxNew = server.getCache().setDocument("cdfMtxNew", cdfMtxNew);
                    var list = cdfMtxNew.selectNodes("//record[@isAssociatedToEnv='false']");
                    var iter = list.iterator();
                    while(iter.hasNext()) {
                        try {
                            var obj = iter.next();
                            cdfMtxNew.removeChild(obj);
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }

                    this.callassociateResourceConfigurationsToEnvironment();
                };

                // Set the edit mode or view mode for the component UI.
                plugin.prototype.toggleMode = function(bEditMode) {
                    if(bEditMode)
                        this.beginEditMode();
                    else
                        this.endEditMode();
                    var server = tibco.admin.Main.getServer();
                    var editTemplate = tibco.admin.Main.getServer().getDynamicProperty("@@ShdResDefs Enabled Edit Mode Value Template");
                    var viewTemplate = tibco.admin.Main.getServer().getDynamicProperty("@@ShdResDefs Enabled View Mode Value Template");
                    this.block.getDescendantOfName("btnEdit_27I").setDisplay(bEditMode ? jsx3.gui.Block.DISPLAYNONE : jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.block.getDescendantOfName("btnCancel_27I").setDisplay(bEditMode ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("btnSave_27I").setDisplay(bEditMode ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
                    this.block.getDescendantOfName("lblInstruction_27I").setDisplay(bEditMode ? jsx3.gui.Block.DISPLAYNONE : jsx3.gui.Block.DISPLAYBLOCK, true);
                    this.block.getDescendantOfName("mcUse_27I").setValueTemplate(bEditMode ? editTemplate : viewTemplate);
                    this.mtx.repaintData();
                };

                /**
                 * Event handler for radio button
                 */
                plugin.prototype.radBtn_onclick = function(id, value) {
                    var server = tibco.admin.Main.getServer();
                    var objCDFDocument = this.mtx.getXML();
                    var objRec = objCDFDocument.selectSingleNode("//record[@jsxid='" + id + "']");
                    if(objRec) {
                        if(objRec.getAttribute("isAssociatedToEnv") != value) {
                            objRec.setAttribute("isAssociatedToEnv", value);
                        }
                    }
                }

                // Get the data from webservice
                plugin.prototype.callgetResourceConfigsInEnvironment = function() {
                    var editStr = "null";
                    if(this.entityId && !this.isInEditMode()) {
                        var objService = tibco.admin.Main.loadService("tibco.admin",
                                "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/rules/getResourceConfigsInEnvironmentRules.xml",
                                "getResourceConfigsInEnvironment",
                                "Get ResourceConfigs");
                        //subscribe and call
                        objService.entityId = this.entityId;
                        var me = this;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetResourceConfigsInEnvironmentSuccess);
                        objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ongetResourceConfigsInEnvironmentError);
                        objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ongetResourceConfigsInEnvironmentInvalid);
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());
                        objService.doCall();
                    }
                };

                plugin.prototype.ongetResourceConfigsInEnvironmentSuccess = function(objEvent) {
                    if(this.mtx.getXML().selectNodes("//record").getLength() > 0) {
                        this.block.getDescendantOfName("btnEdit_27I").setEnabled(jsx3.gui.Form.STATEENABLED, true)
                        this.selectFilter.setEnabled(jsx3.gui.Form.STATEENABLED, true)
                        this.selectEnable.setEnabled(jsx3.gui.Form.STATEENABLED, true)
                        this.tempCacheDoc = this.mtx.getXML().cloneDocument();
                        this.mtx.repaintData();
                        var selectedEnableValue = this.selectEnable.getValue();
                        var selectedFilterValue = this.selectFilter.getValue();
                        if(selectedEnableValue != tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All")
                                || selectedFilterValue != tibco.admin.Main.getServer().getDynamicProperty("@AllTypes")) {
                            this.filterResults();
                        }
                        else {
                            this.mtx.repaintData();
                        }
                    }
                };

                plugin.prototype.ongetResourceConfigsInEnvironmentError = function(objEvent) {
                };

                plugin.prototype.ongetResourceConfigsInEnvironmentInvalid = function(objEvent) {
                };

                // Update the data to webservice
                plugin.prototype.callassociateResourceConfigurationsToEnvironment = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/rules/associateResourceConfigurationsToEnvironmentRules.xml",
                            "associateResourceConfigurationsToEnvironment",
                            "Edit ResourceConfigs");
                    //subscribe and call
                    objService.entityId = this.entityId;
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onassociateResourceConfigurationsToEnvironmentSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onassociateResourceConfigurationsToEnvironmentError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onassociateResourceConfigurationsToEnvironmentInvalid);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());
                    objService.doCall();
                };

                plugin.prototype.onassociateResourceConfigurationsToEnvironmentSuccess = function(objEvent) {
                    this.toggleMode(false, true);
                    this.mode = "VIEW";
                    this.callgetResourceConfigsInEnvironment();
                };

                plugin.prototype.onassociateResourceConfigurationsToEnvironmentError = function(objEvent) {
                };

                plugin.prototype.onassociateResourceConfigurationsToEnvironmentInvalid = function(objEvent) {
                };

                plugin.prototype.onSharedResourcesSelected = function(objEvent) {
                };

                plugin.prototype.publishSelect = function(strRECORDIDS) {
                    var strRowID = strRECORDIDS[0]
                    if(strRECORDIDS.length > 1)
                        return;
                    var record = this.mtx.getXML().selectSingleNode("//record[@jsxid='" + strRowID + "']");
                    if(record) {
                        var msg = new Object();
                        msg.subject = "tibco.admin.enterpriseAssets.envDef.envDtls.shdResDefs.ShdResDefs.SHRESDEFS_ROW_SELECTED"
                        msg.entityId = record.getAttribute("jsxid");
                        msg.hasEdit = record.getAttribute("hasEdit");
                        msg.hasOwner = record.getAttribute("hasOwner");
                        tibco.admin.PubSub.publish(msg);
                    }
                }

                plugin.prototype.onDestroy = function() {
                    this.unsubscribeAll();
                }

                plugin.prototype.filterResults = function() {
                    if(this.tempCacheDoc) {
                        var tempCacheDoc = this.tempCacheDoc;
                    }
                    var filterEnable = this.selectEnable.getText();
                    var filterType = this.selectFilter.getValue();

                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All")) {
                        filterEnable = "All";
                    }
                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All Enabled")) {
                        filterEnable = "true";
                    }
                    if(filterEnable == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All Not Enabled")) {
                        filterEnable = "false";
                    }

                    if(filterType == tibco.admin.Main.getServer().getDynamicProperty("@AllTypes")) {
                        filterType = "All";
                    }
                    this.mtx.setXMLTransformers(tibco.admin.Main.resolveURI("tibco.admin", "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/xsl/transform.xsl"));
                    this.mtx.setXSLParam("enable", filterEnable);
                    this.mtx.setXSLParam("type", filterType);
                    this.mtx.setSourceXML(tempCacheDoc);
                    this.mtx.repaintData();
                }

                plugin.prototype.getComponentPath = function() {
                    return  "tibco/admin/enterpriseAssets/envDef/envDtls/shdResDefs/component.xml"
                }

                plugin.prototype.buttonSelected = function() {
                    this.callgetResourceConfigsInEnvironment();
                }
            });

}